package com.gem.controller.mobile;

import com.gem.domain.CampusConn;
import com.gem.domain.User;
import com.gem.service.CampusConnService;
import com.gem.service.UserService;
import com.gem.util.ExcelUtil;
import com.gem.util.MessageUtil;
import com.gem.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import static com.gem.util.ExcelUtil.setHSSFValidation;

/**
 * @Author: 玲
 * @Description:
 * @create 2019-10-21 15:03
 * @Modified By:
 */
@Controller
@SuppressWarnings("all")
public class MobileCampusConnController {
    @Autowired
    private CampusConnService service;

    @Autowired
    private UserService userService;

    @RequestMapping("/mCampusConn/findAll")
    public String findAll(Integer campusId, Model model,
                          @RequestParam( value = "RcurrentPage",defaultValue = "1") int RcurrentPage,
                          @RequestParam( value = "RpageSize",defaultValue = "5") int RpageSize,
                          @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                          @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        String campusName = service.findCampusName(campusId);
        model.addAttribute("campusName", campusName);
        model.addAttribute("campusId", campusId);
        model.addAttribute("RcurrentPage", RcurrentPage);
        model.addAttribute("RpageSize", RpageSize);
        try {
            PageInfo<CampusConn> pageBeans = service.findAll(campusId, currentPage, pageSize);
            if (pageBeans != null && pageBeans.getList().size() > 0) {
                model.addAttribute("pageBean", pageBeans);
                List<Integer> pageValue = PageUtil.getPageValue(pageBeans.getPages(), currentPage);
                model.addAttribute("pageValue", pageValue);

            } else {
                model.addAttribute("info", "没有数据请先添加！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/campus/infoFindAll", "服务器内部错误！请联系管理员！", model);
            return "mobile_message";
        }
        return "mobile_campus/campusConn";
    }

    @RequestMapping("/mCampusConn/saveUI")
    public String saveUI(String campusName, Integer campusId, Model model,
                         @RequestParam( value = "RcurrentPage",defaultValue = "1") int RcurrentPage,
                         @RequestParam( value = "RpageSize",defaultValue = "5") int RpageSize) {
        model.addAttribute("RcurrentPage", RcurrentPage);
        model.addAttribute("RpageSize", RpageSize);
        try {
            List<User> users = userService.findMmembers();
            model.addAttribute("users", users);
            model.addAttribute("campusName", campusName);
            model.addAttribute("campusId", campusId);
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/campus/infoFindAll", "服务器内部错误！请联系管理员！", model);
            return "mobile_message";
        }
        return "mobile_campus/campusConnInsert";
    }

    @RequestMapping("/mCampusConn/save")
    @ResponseBody
    public String save(CampusConn campusConn, Model model,
                       @RequestParam( value = "RcurrentPage",defaultValue = "1") int RcurrentPage,
                       @RequestParam( value = "RpageSize",defaultValue = "5") int RpageSize) {
        try {
            service.save(campusConn);
           return "1";
        } catch (Exception e) {
            e.printStackTrace();
            return "-1";
        }
    }

    @RequestMapping("/mCampusConn/updateUI")
    public String updateUI(Integer campusConnId, String campusName, Model model,
                           @RequestParam( value = "RcurrentPage",defaultValue = "1") int RcurrentPage,
                           @RequestParam( value = "RpageSize",defaultValue = "5") int RpageSize) {
        model.addAttribute("RcurrentPage", RcurrentPage);
        model.addAttribute("RpageSize", RpageSize);
        try {
            CampusConn campusConn = service.findById(campusConnId);
            model.addAttribute("campusConn", campusConn);
            model.addAttribute("campusName", campusName);
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/CampusConn/findAll" +
                    "&RcurrentPage=" + RcurrentPage + "&RpageSize=" + RpageSize, "服务器内部错误！请联系管理员！", model);
            return "mobile_message";
        }
        return "mobile_campus/campusConnUpdate";
    }

    @RequestMapping("/mCampusConn/update")
    @ResponseBody
    public String update(CampusConn campusConn, Model model,
                         @RequestParam( value = "RcurrentPage",defaultValue = "1") int RcurrentPage,
                         @RequestParam( value = "RpageSize",defaultValue = "5") int RpageSize) {
        try {
           if ( service.update(campusConn) == 1){
               return "1";
           }else {
               return "-1";
           }
        } catch (Exception e) {
            e.printStackTrace();
            return "-1";
        }
    }

    @RequestMapping("/mCampusConn/delete")
    @ResponseBody
    public String delete(Integer campusConnId) {
        try {
            service.deleteById(campusConnId);
            return "1";
        } catch (Exception e) {
            e.printStackTrace();
            return "-1";
        }
    }

}
